package com.atguigu.ssyx.user.service.impl;

import com.atguigu.ssyx.enums.user.Leader;
import com.atguigu.ssyx.enums.user.User;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.order.OrderInfo;
import com.atguigu.ssyx.model.order.OrderItem;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.model.sys.Region;
import com.atguigu.ssyx.user.mapper.LeaderMapper;
import com.atguigu.ssyx.user.mapper.RegionMapper;
import com.atguigu.ssyx.user.mapper.UserMapper;
import com.atguigu.ssyx.user.service.LeaderService;
import com.atguigu.ssyx.user.service.UserService;
import com.atguigu.ssyx.vo.user.LeaderQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class LeaderServiceImpl extends ServiceImpl<LeaderMapper, Leader> implements LeaderService {

    @Autowired
     RegionMapper regionMapper;
    @Override
    public IPage<Leader> selectPageLeader(Page<Leader> pageParam, LeaderQueryVo leaderQueryVo) {
        String keyword= leaderQueryVo.getKeyword();
        LambdaQueryWrapper<Leader> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)) {
            wrapper.like(Leader::getName, keyword);
        }
        IPage<Leader> leaderIPage = baseMapper.selectPage(pageParam, wrapper);
        return leaderIPage;
    }

    @Override
    public IPage<Leader> selectPageCheckList(Page<Leader> pageParam, LeaderQueryVo leaderQueryVo) {
        String keyword= leaderQueryVo.getKeyword();
        LambdaQueryWrapper<Leader> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)) {
            wrapper.like(Leader::getName, keyword);
        }



        IPage<Leader> leaderIPage = baseMapper.selectPage(pageParam, wrapper);
        List<Leader> leaderList = leaderIPage.getRecords();
        for(Leader leader : leaderList) {
            //封装订单状态名称
            Region region = regionMapper.selectById(leader.getProvince());
            leader.getParam().put("provinceName",region.getName());

            Region region1 = regionMapper.selectById(leader.getCity());
            leader.getParam().put("cityName",region1.getName());

            Region region2 = regionMapper.selectById(leader.getDistrict());
            leader.getParam().put("districtName",region2.getName());
        }

        return leaderIPage;
    }

    @Override
    public void saveLeader(Leader leader) {
        baseMapper.insert(leader);
    }

    @Override
    public void updateLeader(Leader leader) {

        baseMapper.updateById(leader);

    }

    @Override
    public Leader getLeader(Long id) {

        Leader leader = baseMapper.selectById(id);

        Region region = regionMapper.selectById(leader.getProvince());
        leader.getParam().put("provinceName",region.getName());

        Region region1 = regionMapper.selectById(leader.getCity());
        leader.getParam().put("cityName",region1.getName());

        Region region2 = regionMapper.selectById(leader.getDistrict());
        leader.getParam().put("districtName",region2.getName());

        return leader;
    }


}
